package com.huabo.fxgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huabo.fxgl.entity.Riskcategory;
import com.huabo.fxgl.mapper.RiskcategoryMapper;
import com.huabo.fxgl.service.IRiskcategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiYe
 * @since 2022-08-01
 */
@Service
public class RiskcategoryServiceImpl extends ServiceImpl<RiskcategoryMapper, Riskcategory> implements IRiskcategoryService {

    @Override
    public Riskcategory findQYFXByOrgid(String orgid, String moduleType) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("unit", orgid);
        if (StringUtils.isNotBlank(moduleType)) {
            queryWrapper.eq("MODULETYPE", moduleType);
        }
        queryWrapper.orderByAsc("riskcatid");

        List<Riskcategory> list = baseMapper.findQYFXByOrgid(queryWrapper);
        if (list!=null && list.size()>0) {
            return list.get(0);
        }
        return null;
    }

    @Autowired
    private RiskcategoryMapper riskcategoryMapper;
    @Override
    public List<Riskcategory> findRiskCateByRoot(BigDecimal rootId) {
        return riskcategoryMapper.findRiskCateByRoot(rootId);
    }
    @Override
    public String[] findRiskCateParentByAssPanid(String asspanid) {
        List<Riskcategory> list = riskcategoryMapper.findRiskCateParentByAssPanid(asspanid);
        String str = "";
        StringBuilder catids = new StringBuilder("");
        for (int i = 0; i < list.size(); i++) {
            Riskcategory cat = list.get(i);
            int isdel = 1;
            if (cat.getRiskcatname()!= null) {
                if ( cat.getRiskcatname().trim().equals("企业风险") || cat.getRiskcatname().trim().equals("业务风险") || cat.getRiskcatname().trim().equals("专项风险")) {
                    isdel = 0;
                }else if (cat.getRiskcatname().trim().equals("风险类型")) {
                    isdel = 2;
                }
            }
            str += "tree.nodes['"+ (i==0?-1:cat.getFatherriskcatid())+"_"+cat.getRiskcatid()+"']=\"text:"+cat.getRiskcatname()+";method:check("+cat.getRiskcatid()+","+(cat.getUnit()==null?-1:cat.getUnit())+","+(cat.getIsleaf()==null?0:cat.getIsleaf())+","+isdel+")\";\n";
            catids.append(cat.getRiskcatid());
            if (i < list.size()-1)
                catids.append(",");
        }
        return new String[]{str, catids.toString()};
    }


    public String findRiskcatByName(Riskcategory riskcat) {
        if (riskcat == null || riskcat.getRiskcatname() == null) {
            return "";
        }
        if(riskcat.getRiskcatname().contains("企业风险")){
            return "企业风险";
        }else if(riskcat.getRiskcatname().contains("专项风险")) {
            return "专项风险";
        }else if(riskcat.getRiskcatname().contains("业务风险")) {
            return "业务风险";
        }
        riskcat = getById(riskcat.getFatherriskcatid());
        return riskcat==null ? null : riskcat.getRiskcatname();

    }
}
